package com.ctyon.watchmanager.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.ctyon.watchmanager.R;
import com.ctyon.wheelpicker.base.WheelItem;
import com.ctyon.wheelpicker.base.WheelItemView;

import androidx.annotation.NonNull;

/**
 * CreateDate：19-7-22 on 上午10:01
 * Describe:
 * Coder: lee
 */
public class AlarmDialog extends Dialog implements View.OnClickListener {

    private final static String TAG = "AlarmDialog";

    private WheelItemView alarmPickerHour;
    private WheelItemView alarmPickerMinute;
    private String hour, min;
    private AlarmPickerCallback alarmPickerCallback;
    private RadioGroup radioGroup;
    private RadioButton radioButton1, radioButton2;
    private Button button1, button2, button3, button4, button5, button6, button7;
    private int[] dateArr = new int[7];
    private int type;
    private boolean isClean;
    private boolean isOnce = true;

    public AlarmDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_alarm);

        alarmPickerHour = findViewById(R.id.picker_hour);
        alarmPickerMinute = findViewById(R.id.picker_minute);

        alarmPickerHour.setOnSelectedListener((context, selectedIndex) -> hour = selectedIndex < 10 ? "0" + selectedIndex : "" + selectedIndex);
        alarmPickerMinute.setOnSelectedListener((context, selectedIndex) -> min = selectedIndex < 10 ? "0" + selectedIndex : "" + selectedIndex);

        radioGroup = findViewById(R.id.alarm_picker_rp);
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            Log.d(TAG, "onCheckedChanged : " + group.getId() + "  " + checkedId);
            if (checkedId == radioButton1.getId() && !isClean) {//一次
                dateArr = new int[7];
                for (int i = 0; i < 7; i++) {
                    setSelect(i + 1, false);
                }
                type = 1;
            } else if (checkedId == radioButton2.getId() && !isClean) {//每天
                for (int i = 0; i < 7; i++) {
                    dateArr[i] = 1;
                    setSelect(i + 1, true);
                }
                type = 2;
            } else {
                isClean = false;
                type = 3;
            }

        });

        radioButton1 = findViewById(R.id.alarm_picker_once);
        radioButton2 = findViewById(R.id.alarm_picker_every);

        button1 = findViewById(R.id.alarm_date_1);
        button2 = findViewById(R.id.alarm_date_2);
        button3 = findViewById(R.id.alarm_date_3);
        button4 = findViewById(R.id.alarm_date_4);
        button5 = findViewById(R.id.alarm_date_5);
        button6 = findViewById(R.id.alarm_date_6);
        button7 = findViewById(R.id.alarm_date_7);

        button1.setOnClickListener(this::onClick);
        button2.setOnClickListener(this::onClick);
        button3.setOnClickListener(this::onClick);
        button4.setOnClickListener(this::onClick);
        button5.setOnClickListener(this::onClick);
        button6.setOnClickListener(this::onClick);
        button7.setOnClickListener(this::onClick);

        findViewById(R.id.picker_cancel).setOnClickListener(v -> cancel());
        findViewById(R.id.picker_submit).setOnClickListener(v -> {
            if (null != alarmPickerCallback) {
                StringBuilder builder = new StringBuilder();
                for (int i : dateArr) {
                    builder.append(i);
                }
                String alarm = hour + "*" + min + "-1-" + type + "-" + builder.toString();
                alarmPickerCallback.alarmCallback(alarm);
            }
            cancel();
        });

        loadHour();
        loadMinute();
    }

    private void loadHour() {
        WheelItem[] items = new WheelItem[24];
        for (int i = 0; i < 24; i++) {
            if (i < 10) {
                items[i] = new WheelItem("0" + i);
            } else {
                items[i] = new WheelItem("" + i);
            }
        }
        alarmPickerHour.setItems(items);
        alarmPickerHour.setSelectedIndex(0);
    }

    private void loadMinute() {
        WheelItem[] items = new WheelItem[60];
        for (int i = 0; i < 60; i++) {
            if (i < 10) {
                items[i] = new WheelItem("0" + i);
            } else {
                items[i] = new WheelItem("" + i);
            }
        }
        alarmPickerMinute.setItems(items);
        alarmPickerMinute.setSelectedIndex(0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.alarm_date_1:
                if (button1.isSelected()) {
                    button1.setSelected(false);
                    dateArr[0] = 0;
                } else {
                    button1.setSelected(true);
                    dateArr[0] = 1;
                }
                cleanRadio();
                break;
            case R.id.alarm_date_2:
                if (button2.isSelected()) {
                    button2.setSelected(false);
                    dateArr[1] = 0;
                } else {
                    button2.setSelected(true);
                    dateArr[1] = 1;
                }
                cleanRadio();
                break;
            case R.id.alarm_date_3:
                if (button3.isSelected()) {
                    button3.setSelected(false);
                    dateArr[2] = 0;
                } else {
                    button3.setSelected(true);
                    dateArr[2] = 1;
                }
                cleanRadio();
                break;
            case R.id.alarm_date_4:
                if (button4.isSelected()) {
                    button4.setSelected(false);
                    dateArr[3] = 0;
                } else {
                    button4.setSelected(true);
                    dateArr[0] = 1;
                }
                cleanRadio();
                break;
            case R.id.alarm_date_5:
                if (button5.isSelected()) {
                    button5.setSelected(false);
                    dateArr[4] = 0;
                } else {
                    button5.setSelected(true);
                    dateArr[4] = 1;
                }
                cleanRadio();
                break;
            case R.id.alarm_date_6:
                if (button6.isSelected()) {
                    button6.setSelected(false);
                    dateArr[5] = 0;
                } else {
                    button6.setSelected(true);
                    dateArr[5] = 1;
                }
                cleanRadio();
                break;
            case R.id.alarm_date_7:
                if (button7.isSelected()) {
                    button7.setSelected(false);
                    dateArr[6] = 0;
                } else {
                    button7.setSelected(true);
                    dateArr[6] = 1;
                }
                cleanRadio();
                break;
        }
    }

    public interface AlarmPickerCallback {
        void alarmCallback(String alarm);
    }

    public void setAlarmPickerCallback(AlarmPickerCallback alarmPickerCallback) {
        this.alarmPickerCallback = alarmPickerCallback;
    }

    public void setTime(int hour, int minute, int legacy, String date) {
        isClean = false;
        isOnce = true;
        alarmPickerHour.setSelectedIndex(hour);
        alarmPickerMinute.setSelectedIndex(minute);
        cleanRadio();
        if (null == date || legacy == 1) {
            radioButton1.setChecked(true);
        } else if (legacy == 2) {
            radioButton2.setChecked(true);
        } else {
            for (int i = 0; i < date.length(); i++) {
                char ch = date.charAt(i);
                dateArr[i] = ch - '0';
                if (dateArr[i] == 1) {
                    setSelect(i + 1, true);
                } else {
                    setSelect(i + 1, false);
                }
            }
            cleanRadio();
        }
    }

    private void setSelect(int index, boolean select) {
        switch (index) {
            case 1:
                button1.setSelected(select);
                break;
            case 2:
                button2.setSelected(select);
                break;
            case 3:
                button3.setSelected(select);
                break;
            case 4:
                button4.setSelected(select);
                break;
            case 5:
                button5.setSelected(select);
                break;
            case 6:
                button6.setSelected(select);
                break;
            case 7:
                button7.setSelected(select);
                break;
        }
    }

    private void cleanRadio() {
        isClean = true;
        radioGroup.clearCheck();
        for (int i : dateArr) {
            if (i == 1) {
                isOnce = false;
                break;
            }
        }
        if (isOnce) {
            radioButton1.setChecked(true);
        }
    }

}
